﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;

public partial class verify_tkt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        litTicket.Text = "";
    }
    protected void btnVerify_Click(object sender, EventArgs e)
    {
        if (txtTktNo.Text.Length != 14)
        {
            lblMessage.Text = "非法票号！";
            return;
        }
        string s_bk_id,s_pax_sn;
        long bk_id;
        int pax_sn;
        s_bk_id = txtTktNo.Text.Substring(0, 11);
        s_pax_sn = txtTktNo.Text.Substring(11, 3);
        try
        {
            bk_id = Convert.ToInt64(s_bk_id);
            pax_sn = Convert.ToInt32(s_pax_sn);
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            return;
        }

        CRSI.Ticket inst = new CRSI.Ticket();
        int ret_code;
        string ret_msg;
        DataSet dsRet;

        int last_pax_sn = -1, curr_pax_sn;//, last_trip_sn=-1;
        string curr_tkt_no = "", buf2;
        StringBuilder buf = new StringBuilder();
        string sell_office = "", entity = "";
        string pay_currency = "", pay_time = "";

        float total_fare = 0, fare_roe = 1, tax_roe = 1;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(bk_id.ToString() + "|" + pax_sn.ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        dsRet = inst.VerifyTicket(bk_id,pax_sn,/*txtFamilyName.Text,txtName.Text,*/txtIdNo.Text, 2, Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblMessage.Text = ret_msg;
            return;
        }
        else if (ret_code > 0)
        {
            switch (ret_code)
            {
                case 1:
                    Response.Write("订单找不到");
                    break;
                case 2:
                    Response.Write("无权查看");
                    break;
                case 3:
                    Response.Write("无查看本Office订单权限");
                    break;
                case 4:
                    Response.Write("无查看本Entity订单权限");
                    break;
                case 91:
                    Response.Write("登录错");
                    break;
                default:
                    Response.Write("未知错误：" + ret_code.ToString());
                    break;
            }
            return;
        }
        if (dsRet.Tables[0].Rows.Count == 0)
        {
            lblMessage.Text = "没有找到符合的机票！";
            return;
        }
        else
        {
            lblMessage.Text = "发现机票。请核对航程";
        }
        foreach (DataRow row in dsRet.Tables[0].Rows)
        {
            curr_pax_sn = Convert.ToInt32(row["pax_sn"]);
            if (curr_pax_sn != last_pax_sn)
            {
                if (last_pax_sn != -1)
                {
                    //Response.Write("<tr><td colspan='5' align='center'>" + bar_code128A(curr_tkt_no, 20, 1, 0) + "<br/>票号/Tkt No.: " + curr_tkt_no + "</td><td colspan='2'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='3'>出票点/Agent:<br/>"/* + office_name + "(" */+ entity + "/" + sell_office + "</td><td colspan='3'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
                    buf.Append("<tr><td colspan='5' align='center'><!--img src='bar_code.aspx?source=" + curr_tkt_no + "&height=15&unit_width=1'><br/-->票号/Tkt No.: " + curr_tkt_no + "</td><td colspan='2'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='3'>出票点/Agent:<br/>"/* + office_name + "(" */+ entity + "/" + sell_office + "</td><td colspan='3'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
                    buf.Append("<tr><td colspan='13'>验真网站/Verify Site: http://www.mysite.com </td></tr>");
                    buf.Append("</table><br/><img src='scissors.bmp'><hr style='border:1px dashed black; height:1px' width='50%'><br/>");
                    total_fare = 0;
                }
                buf.Append("<table style='border-collapse:collapse' border=1 borderColor='black'><tr><td colspan='13' align='center'><b><br/>行程提醒单/ITINERARY<br/>&nbsp;</b></td></tr><tr><td colspan='3' align='left'>姓名/Name:<br/>");
                buf.Append(row["family_name"].ToString().Trim() + "/" + row["name"].ToString().Trim() + "<br/>" + row["family_name_en"].ToString().Trim() + "/" + row["name_en"].ToString().Trim() + "</td>");
                //Response.Write("<td colspan='2'>类型/Pax. Type:<br/>" + row["age_type"] + "</td><td colspan='7' align='center'>" + bar_code128A(row["id_no"].ToString(), 25, 1, 0) + "<br/>证件号/ID: " + row["id_no"].ToString().Trim() + "</td></tr>");
                buf.Append("<td colspan='2'>年龄/Age:<br/>" + row["age_type"] + "</td><td colspan='2'>有效期至/Expiry:<br/>" + Convert.ToDateTime(row["expiry"]).ToShortDateString() + "</td><td colspan='6' align='center'><img src='bar_code.aspx?source=" + row["id_no"].ToString() + "&height=20&unit_width=1'><br/>证件号/ID: " + row["id_no"].ToString().Trim() + "</td></tr>");
                //Response.Write("<tr><td colspan='4'>支付时间/Pay Time:" + row["pay_time"].ToString() + "</td><td colspan='3'>支付币种/Currency:" + row["pay_currency"] + "</td><td colspan='3'>票价兑换率/Fare ROE:" + row["fare_roe"] + "</td><td colspan='3'>税兑换率/Tax ROE:" + row["tax_roe"] + "</td></tr>");
                //Response.Write("<td colspan='2'>Order: " + Request.QueryString["bk_id"].ToString() + "</td></tr>");

                //tabReceipts = new Table();
                //tabRow = new TableRow();
                //tabCell = new TableCell();
                last_pax_sn = curr_pax_sn;
                //last_trip_sn = -1;
                buf.Append("<tr><td>S/N</td><td>航程<br/>From-To</td><td>航班<br/>Flight</td><td>日期<br/>Date</td><td>起飞时间<br/>Dep. At</td><td>到达时间<br/>Arr. At</td><td>舱位/子舱<br/>Cabin/Class</td><td>票价代码<br/>Fare Code</td><td>票价<br/>Tkt. Price</td><td>附加费<br/>Fuel Charge</td><td>税<br/>Taxes</td><td>免费行李<br/>Bag. Allow</td></tr>");//<tr><td>&nbsp;</td><td colspan='2'>Baggage Allow</td><td colspan='4'>Check-in No.</td></tr>");
            }
            //if(last_trip_sn==-1)
            //    Response.Write("<tr><td>S/N</td><td>From-To</td><td>Flight</td><td>Cabin/Class</td><td>Dep. Time</td><td>Fare Code</td><td>Tkt. Price</td><td>Taxes</td><td>Fuel Charge</td></tr>");//<tr><td>&nbsp;</td><td colspan='2'>Baggage Allow</td><td colspan='4'>Check-in No.</td></tr>");

            //tkt_fare = Convert.ToSingle(row["final_price"]);
            //taxes = Convert.ToSingle(row["final_taxes"]);
            //fuel_charge = Convert.ToSingle(row["final_fuel"]);
            //total_fare = total_fare + tkt_fare + taxes + fuel_charge;
            sell_office = row["pay_office"].ToString();
            //office_name = row["name"].ToString().Trim();
            //pay_time = row["pay_time"].ToString();
            entity = row["pay_airline"].ToString();
            buf2 = "000" + row["pax_sn"].ToString();
            buf2 = buf2.Substring(buf2.Length - 3);
            curr_tkt_no = Convert.ToInt64(row["bk_id"]).ToString() + buf2;
            fare_roe = Convert.ToSingle(row["fare_roe"]);
            //tax_roe = Convert.ToSingle(row["tax_roe"]);
            pay_currency = row["pay_currency"].ToString();
            pay_time = row["pay_time"].ToString();

            buf.Append("<tr><td>" + row["trip_sn"].ToString() + "</td><td>" + row["ori_city"] + "- " + row["des_city"] + "</td><td>" + row["airline"] + row["flt_no"] + "</td><td>" + Convert.ToDateTime(row["dep_time"]).ToShortDateString() + "</td>");
            buf.Append("<td>" + Convert.ToDateTime(row["dep_time"]).ToShortTimeString() + "</td><td>" + Convert.ToDateTime(row["arr_time"]).ToShortTimeString() + "</td><td>" + row["cabin"] + "/" + row["class"] + "</td><td>" + row["fare_code"] + "</td><td>" + row["fare_currency"] + Convert.ToSingle(row["final_price"]).ToString() + "</td><td>" + row["fare_currency"] + Convert.ToSingle(row["final_fuel"]).ToString() + "</td><td>" + Convert.ToSingle(row["final_taxes"]).ToString() + "</td>");
            total_fare = total_fare + (Convert.ToSingle(row["final_price"]) + Convert.ToSingle(row["final_fuel"]) + Convert.ToSingle(row["final_taxes"])) * Convert.ToSingle(row["fare_roe"]); //+ Convert.ToSingle(row["final_taxes"]) * Convert.ToSingle(row["tax_roe"]);
            //Response.Write("<tr><td>&nbsp;</td><td>Baggage Allow:<br/>");
            buf.Append("<td>");
            if (row["age_type"].ToString() == "A")
                buf.Append(row["free_baggage"]);
            else if (row["age_type"].ToString() == "C")
                buf.Append(row["chd_baggage"]);
            else if (row["age_type"].ToString() == "I")
                buf.Append(row["inf_baggage"]);
            //tkt_no = Request.QueryString["bk_id"].ToString() + "-" + row["pax_sn"].ToString().PadLeft(3, '0') + "-" + row["trip_sn"].ToString();
            buf.Append("</td></tr>");//<td colspan='7' align='center'>" + bar_code128A(row["id_no"].ToString(), 25, 1, 0) + "<br/>Self-service Code: " + row["id_no"].ToString() + "</td></tr>");

        }
        //Response.Write("<tr><td colspan='5' align='center'>" + bar_code128A(curr_tkt_no, 20, 1, 0) + "<br/>票号/Tkt No.: " + curr_tkt_no + "</td><td colspan='2'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='3'>出票点/Agent:<br/>"/* + office_name + "(" */+ entity + "/" + sell_office + "</td><td colspan='3'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
        buf.Append("<tr><td colspan='5' align='center'><!--img src='bar_code.aspx?source=" + curr_tkt_no + "&height=15&unit_width=1'><br/-->票号/Tkt No.: " + curr_tkt_no + "</td><td colspan='2'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='3'>出票点/Agent:<br/>"/* + office_name + "(" */+ entity + "/" + sell_office + "</td><td colspan='3'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
        buf.Append("<tr><td colspan='13'>验真网站/Verify Site: http://www.mysite.com </td></tr>");
        buf.Append("</table>&nbsp;<br/>&nbsp;<br/>");
        litTicket.Text = buf.ToString();
    }
}
